<?php
header("Content-type: image/jpeg");
ini_set('display_errors', 'Off'); //不能有错误发生，包括显示或不显示的（注意:1/3）


$url=$_GET['url'];
$url=(!preg_match("/https?\:\/\//Uisx", $url)) ? urldecode($url) : $url;

//$url="httpsmmbiz___qpic___cn/mmbiz_jpg/nibEqsKGVcbBeyia0u5TGfItiaYgvxSnkMRu3hLUNSuCwq7RmbsePZJriasnriah4tibpAAEP4AWdLUdkZ3mx7a34hxw/640____wx_fmt=jpeg";
$url=str_replace("____","?",$url);
$url=str_replace("___",".",$url);
$url=str_replace("__","%",$url);
//$url=str_replace("_","&",$url);//不能打开啊
$url=preg_replace("/https(?!\:)/Uisx","https://",$url);
$url=preg_replace("/http(?!s)(?!\:)/Uisx","http://",$url);
$url=preg_replace("/^[0-9]+/isx","",$url);
$url=preg_replace("/^\/http/isx","http",$url);
//echo $url;exit;


$filename = basename($url);


//header ( 'Content-Disposition: attachment; filename="' . rawurlencode ( $originfile ) . '"' );
//header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode ( $originfile ));
//header("Content-Length: ".filesize($filename));
//echo $url;exit;
$url=preg_replace("/(https?)\:\/(?!\/)/isx","\\1://",urldecode($url));
$c=$_REQUEST['c'];
$Referer=urldecode($_REQUEST['r']); //可用指定的，用自已的有时也不行（注意:2/3）
$Referer=($Referer=="")?"http{$htts}://".$host:$Referer;

$htts=preg_match("/https/i",$url)?"s":"";
$url=preg_replace("/\s*https?\:\/\/.*https?\:\/\//isx","http{$htts}://",$url);
preg_match("/https?\:\/\/(.*)\//Uisx",$url,$nn);$host=$nn[1];

$url=str_replace("BAIFENHAO","%",$url);
$haveReferer=1;
if(preg_match("/sinaimg\.cn/Uisx",$url)){
  $haveReferer=0;
}

if(preg_match("/hbimg\.b0\.upaiyun\.com/Uisx",$url)){
    $Referer="http://huaban.com/boards/29652298/";
}

if(preg_match("/zwfw\.gxzf\.gov\.cn/Uisx",$url)){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch, CURLOPT_VERBOSE,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host:  bstl.zwfw.gxzf.gov.cn","Connection:  keep-alive","Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3","Content-Type:  multipart/form-data; boundary=----WebKitFormBoundaryutaAntxhyaHAyJHI","Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7"));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $result=curl_exec($ch);
    curl_close($ch);
    echo $result;exit;
}

if(preg_match("/39\.com/Uisx",$url)){
  $cookie="_wt_session_refer_cookie=http%253A%252F%252Fwww.39.net%252F; Hm_lpvt_ab2e5965345c61109c5e97c34de8026a=1350459594308; Hm_lpvt_1827105cf5cfac624c8dbf5159abfdb4=1350459576311; onlineId=4770284101207144433; area_info=CN450100|%D6%D0%B9%FA|%B9%E3%CE%F7|%C4%CF%C4%FE%CA%D0|%B5%E7%D0%C5; Hm_lvt_ab2e5965345c61109c5e97c34de8026a=1350459575708; Hm_lvt_1827105cf5cfac624c8dbf5159abfdb4=1350459576311";
}

if(preg_match("/photo\.sina\.com\.cn/Uisx",$url)){
  $cookie="U_TRS1=0000002a.5ede4b18.51a9ace5.29e7cc12; U_TRS2=0000002a.5eeb4b18.51a9ace5.a6fa6e22; UOR=,club.edu.sina.com.cn,; Apache=5411857267557.991.1370074365751; SINAGLOBAL=5411857267557.991.1370074365751; ULV=1370074365762:1:1:1:5411857267557.991.1370074365751:";
}


if(preg_match("/qq\.com|qpic\.cn|sogoucdn\.com/Uisx",$url)){
    $cookie="pgv_pvi=9041807360; pt2gguin=o0065391446; RK=WW2uxASmX+; ptcz=ede34bae9f3cb7f1afb93abb9be466e65844e4a718997b8ef3cada7cdf00e5fc; pgv_pvid=8213005312";
    $Referer="https://mp.weixin.qq.com";
    //$url=str_replace("https://","http://",$url);
}


$cookie=($cookie!="")?"\r\nCookie: ".trim($cookie)."":"";

if($c){
    //加专用的cookie和httpheader，有两种取法渠道，一域名是图片服务器的，另一个域名是引用图片服务器的（注意:3/3）
    function host($str)
    {
        $str = urldecode($str);
        preg_match_all("/https?\:\/\/(?:[^<>\/]*?)([a-z0-9_]+?\.(?:com|gov|edu|org|mobi|la|us|hk|tw|net|cn|vn|cc|info|top|vip|co|site|me|xyz)(?:(?:\.net|\.cn|\.cc|\.tv|\.hk|\.tw|\.info|\.vn|\.top|\.vip)*(?=\:|\/|$)))/isx", $str, $nn);
        $host = $nn[1][0];
        $host = preg_replace("/^(?:[^<>]*\.)*([a-z0-9_]+\.(com|gov|edu|org|cn|mobi|la|hk|tw|us|cc|info|vn|top|vip|co|site|me|xyz))/Uisx", "\\1", $host);
        if ($host == "" && preg_match("/https?\:\/\/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $str, $nn)) {
            $host = $nn[1];
        }
        return $host;
    }

    function httpheadertoarray($s)
    {
        $s=preg_replace("/\"(.*)\"(\,|\s|$)/Uisx","\\1".PHP_EOL,$s);
        $arr = preg_split("/\s*(\r|\n)/im", $s);
        $s2="array(";
        for($i=0;$i<count($arr);$i++){
            if($arr[$i]==""||$arr[$i]==null) unset($arr[$i]);
            $s2.="\"{$arr[$i]}\",";
        }
        $s2=substr($s2,0,-1).")";
        return $arr;
    }

  $cn=mysqli_connect("127.0.0.1","root","159456","longzs");
  $ku=mysqli_query($cn,"select * from longzs_re where `host`='".host($url)."'");
  $rs=mysqli_fetch_array($ku,1);
  $options=explode(",",$rs['nrzq']);
  $cookie=$rs['cookie'];
}

//$file=$_SERVER['DOCUMENT_ROOT']."/900.jpg";
//list($width, $height) = getimagesize($file);
//$src_im = @imagecreatefromjpeg($file);
//$dst_im = imagecreatetruecolor($width, $height);
////imagecopy( $dst_im, $src_im, 0, 0, 0, 0, $width, $height);
//imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $width, $height, $width, $height);
//imagejpeg($dst_im);
//imagedestroy($dst_im);
//imagedestroy($src_im);
//file_put_contents($_SERVER['DOCUMENT_ROOT']."/public.jpg",$nr);
//$nr=file_get_contents($_SERVER['DOCUMENT_ROOT']."/900.jpg");
//readfile($_SERVER['DOCUMENT_ROOT']."/900.jpg");
//$fn=fopen($_SERVER['DOCUMENT_ROOT']."/public.jpg","r");
//$nr=fread($fn,filesize($_SERVER['DOCUMENT_ROOT']."/public.jpg"));
//fclose($fn);

$nr=curl98($url,$cookie,$Referer,$rs);
//
if(trim($nr)==""){
    echo $url;
} else{
    echo $nr;
}


function curl98($url, $cookie="",$Referer="",$rs)
{
    global $host;
    $htts=preg_match("/https/i",urldecode($url))?"s":"";
    $timeout = 15;
    $url=trim(chop($url));
    $option=explode(",",$rs['nrzq']);

    //$url = str_replace("%", "%25", $url);
    $Referer=($Referer=="")?"http{$htts}://".$host."/":urldecode($Referer);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, trim(chop($url)));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, "false");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, "2");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
    if($cookie&&!in_array("1",$option)){
        curl_setopt($ch, CURLOPT_COOKIE, trim($cookie));
    }

    if($Referer&&!in_array("2",$option)) {
        curl_setopt($ch, CURLOPT_REFERER, trim($Referer));
    }
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    //$rs['httpheadoption']='array("Connection:  keep-alive","Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","User-Agent:  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5","Cache-Control:max-age=0","Upgrade-Insecure-Requests:1","DNT:1")';
    if (trim($rs['httpheadoption']) != "") {
        curl_setopt($ch, CURLOPT_HTTPHEADER, httpheadertoarray($rs['httpheadoption']));
        //curl_setopt($ch, CURLOPT_HTTPHEADER,  array("Connection:  keep-alive","Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","User-Agent:  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","Accept-Language:  en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6,bg;q=0.5","Cache-Control:max-age=0","Upgrade-Insecure-Requests:1","DNT:1"));
    }else {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
            'Accept-Encoding: gzip, deflate',
            'Connection: Keep-Alive',
        ));
    }
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    ob_start();
    curl_exec($ch);
    curl_close($ch);
    $result = ob_get_contents();
    ob_end_clean();
//    file_put_contents("C:\\Users\\yangzs\\Desktop\\99.jpg",$result);
//    $result=file_get_contents("C:\\Users\\yangzs\\Desktop\\99.jpg");
//    if(preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis", $result, $nn)) {
//        $result = $nn[2];
//        if (@gzinflate(substr($result, 10))) {
//            $result = gzinflate(substr($result, 10));
//        }
//    }
    return $result;
}


?>